﻿using Newtonsoft.Json;
using System;

namespace CopyDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
			Member member = new Member()
			{
				UserName = "用户名",
				NickName = "昵称",
				Age = 1,

				Address = new Address()
				{
					Provice = "上海",
					City = "上海市",
					Code =	1,
				}
			};

			//浅拷贝
			Member memberClone = (Member)member.Clone();
			memberClone.Age = 2;
			memberClone.UserName = "浅拷贝用户名";
			memberClone.Address.Provice = "浅拷贝上海";
			memberClone.Address.Code = 2;

			//通过序列化、反序列化实现深拷贝
			Member depthMember = JsonConvert.DeserializeObject<Member>(JsonConvert.SerializeObject(member));
			depthMember.Age = 3;
			depthMember.UserName = "深拷贝用户名";
			depthMember.Address.Provice = "深拷贝上海";
			depthMember.Address.Code = 3;

			Console.WriteLine($"member：{JsonConvert.SerializeObject(member)}");
			Console.WriteLine($"memberClone：{JsonConvert.SerializeObject(memberClone)}");
			Console.WriteLine($"depthMember：{JsonConvert.SerializeObject(depthMember)}");
		}
    }
	public class Member : ICloneable
	{
        public int Age { get; set; }
        public string UserName { get; set; }

		public string NickName { get; set; }

		public Address Address { get; set; }

		public object Clone()
		{
			return this.MemberwiseClone();
		}
	}

	public class Address
	{
		public string Provice { get; set; }

		public string City { get; set; }
        public int Code { get; set; }
    }
}
